package com.zero.clinic.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zero.clinic.pojo.*;
import com.zero.clinic.service.DeptService;
import com.zero.clinic.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/dept")
public class DeptController {
    @Resource
    private DeptService deptService;
    @Resource
    private UserService userService;

    /**
     * @function 进入科室管理模块
     * @return
     */
    @RequiresPermissions("dept:deptPage")
    @RequestMapping("/deptPage")
    public String deptPage(){
        return "deptPage";
    }

    /**
     * @function 科室列表
     * @param pageNum
     * @param pageSize
     * @param keywords
     * @return
     */
    @ResponseBody
    @RequiresPermissions("dept:list")
    @RequestMapping("/list")
    public PageInfo<Dept> deptList(@RequestParam(defaultValue = "1") int pageNum,
                                   @RequestParam(defaultValue = "10") int pageSize, String keywords){
        PageHelper.startPage(pageNum, pageSize);
        // 1.查出所有的Dept信息
        DeptExample example = new DeptExample();
        // 2 根据搜索关键词查询
        if (StringUtils.isNotBlank(keywords)){
            // 2.1 根据根据科室查询
            DeptExample.Criteria criteria= example.createCriteria();
            criteria.andDeptNameLike("%"+ keywords +"%");
        }
        List<Dept> depts = deptService.selectByExample(example);
        // 3.以PageInfo对象返回给前端
        PageInfo<Dept> pageInfo = new PageInfo<>(depts);
        return pageInfo;
    }

    /**
     * @function 编辑科室页面
     * @param deptId
     * @param m
     * @return
     */
    @RequestMapping("/edit")
    public String deptEdit(Long deptId, Model m){

        // 1.如果前端页面需要更新科室信息，则会传一个需要更新的deptId过来。这里需要通过deptId查出更新信息后共享给前端页面
        if (deptId != null) {
            Dept dept = deptService.selectByPrimaryKey(deptId);
            m.addAttribute("dept",dept);
        }

        return "deptEdit";
    }

    /**
     * @function 新增科室
     * @param dept
     * @return
     */
    @ResponseBody
    @RequestMapping("/insert")
    @RequiresPermissions("dept:insert")
    public MessageObject deptInsert(Dept dept){
        //1.插入数据
        System.out.println("需要新增的科室：" + dept);
        int row = deptService.insert(dept);
        if (row > 0) {
            return MessageObject.createMo(1,"新增数据成功");
        }
        return MessageObject.createMo(0,"新增数据失败，请联系技术人员");
    }

    /**
     * @function 更新科室
     * @param dept
     * @return
     */
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("dept:update")
    public MessageObject deptUpdate(Dept dept){
        //1.更新数据
        System.out.println("需要更新的科室：" + dept);
        int row = deptService.updateByPrimaryKeySelective(dept);
        if (row > 0) {
            return MessageObject.createMo(1,"更新数据成功");
        }
        return MessageObject.createMo(0,"更新数据失败，请联系技术人员");
    }

    /**
     * @function 检查科室名是否重复
     * @param deptName
     * @return
     */
    @ResponseBody
    @RequestMapping("/checkDeptName")
    @RequiresPermissions("dept:checkDeptName")
    public boolean deptCheckDeptName(String deptName){
        DeptExample example = new DeptExample();
        DeptExample.Criteria criteria = example.createCriteria();
        criteria.andDeptNameEqualTo(deptName);
        List<Dept> depts = deptService.selectByExample(example);
        return depts.size() > 0 ? false : true;
    }

    /**
     * @function 删除科室
     * @param deptId
     * @return
     */
    @ResponseBody
    @RequestMapping("/delete")
    @RequiresPermissions("dept:delete")
    public MessageObject deptDelete(Long deptId){
        //1.删除数据前判断该科室下面是否还有员工，如果有则不能删除
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andDeptIdEqualTo(deptId);
        List<User> users = userService.selectByExample(userExample);
        if (users.size() > 0){
            return MessageObject.createMo(0, "该科室下还有员工，不能删除");
        }

        //2.根据deptId删除数据
        int row = deptService.deleteByPrimaryKey(deptId);
        if (row > 0){
            return MessageObject.createMo(1,"删除数据成功");
        }
        return MessageObject.createMo(0,"删除数据失败，请联系技术人员");
    }

    /**
     * @function 批量删除科室
     * @param deptIds
     * @return
     */
    @ResponseBody
    @RequestMapping("/batchdel")
    @RequiresPermissions("dept:batchdel")
    public MessageObject deptBatchDelete(String deptIds){
        //1.删除数据前判断用户下面是否还有下属，如果有则不能删除
        String[] deptIdsArr = deptIds.split(",");
        for (String deptId : deptIdsArr) {
            UserExample userExample = new UserExample();
            UserExample.Criteria criteria = userExample.createCriteria();
            criteria.andDeptIdEqualTo(Long.parseLong(deptId));
            List<User> users = userService.selectByExample(userExample);
            if (users.size() > 0){
                return MessageObject.createMo(0, "被删除的科室下还有员工，不能删除");
            }
        }

        //2.根据deptIdsArr删除数据
        int row = deptService.deleteByIds(deptIdsArr);
        if (row > 0){
            return MessageObject.createMo(1,"删除数据成功");
        }
        return MessageObject.createMo(0,"删除数据失败");
    }


}
